﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;

namespace Artisan.Server
{
    public partial class FormMain : Form
    {
        private String _hostUrl = "";
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            String path = System.AppDomain.CurrentDomain.BaseDirectory + "\\htdocs";
            ToolkitServer server = new ToolkitServer( 4050, path);
            Thread httpThread = new Thread(new ThreadStart(server.listen));
            httpThread.Start();

            pictureBox1.Image = GenByZXingNet(hostUrl());
        }
        private String hostUrl()
        {
            if (String.IsNullOrEmpty(_hostUrl))
            {
                string hostName = Dns.GetHostName();//本机名   
                System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;//会警告GetHostByName()已过期，我运行时且只返回了一个IPv4的地址   
                                                                                             //System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//会返回所有地址，包括IPv4和IPv6   
                                                                                             //foreach (IPAddress ip in addressList)  
                                                                                             //{
                IPAddress ip = addressList[0];
                String ipstr = ip.ToString();
                _hostUrl = "http://" + ipstr + ":4050/";
            }
            return _hostUrl;
        }
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="msg">二维码信息</param>
        /// <returns>图片</returns>
        private Bitmap GenByZXingNet(string msg)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码问题
            writer.Options.Hints.Add(
                EncodeHintType.ERROR_CORRECTION,
                ZXing.QrCode.Internal.ErrorCorrectionLevel.H

            );
            const int codeSizeInPixels = 200;   //设置图片长宽
            writer.Options.Height = writer.Options.Width = codeSizeInPixels;
            writer.Options.Margin = 1;//设置边框
            ZXing.Common.BitMatrix bm = writer.Encode(msg);
            Bitmap img = writer.Write(bm);
            return img;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.linkLabel1.Links[0].LinkData = hostUrl();
            System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Environment.Exit(0);
        }
    }
}

